package org.acooly.secretbox.ui;

import java.util.ArrayList;
import java.util.List;

import org.acooly.android.utils.StringUtils;
import org.acooly.android.utils.ToastUtils;
import org.acooly.secretbox.R;
import org.acooly.secretbox.common.BaseActivity;
import org.acooly.secretbox.service.AccountTypeService;
import org.acooly.secretbox.service.domain.AccountType;
import org.acooly.secretbox.service.impl.AccountTypeServiceImpl;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class AccountTypeActivity extends BaseActivity {

	private AccountTypeService accountTypeService;

	/** 账户类型 */
	private List<AccountType> accountTypes = new ArrayList<AccountType>();
	private AccountTypeAdapter adapter;
	private ListView listView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.accounttype);
		accountTypeService = new AccountTypeServiceImpl(this);

		ImageView accountTypeAddButton = (ImageView) findViewById(R.id.account_type_manage_add);
		accountTypeAddButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showAddView();
			}
		});

		Button backButton = (Button) findViewById(R.id.navigation_back);
		backButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		// 账户类型
		listView = (ListView) findViewById(R.id.account_type_list);
		accountTypes = accountTypeService.getAll();
		adapter = new AccountTypeAdapter(this, accountTypes, R.layout.account_type_manage_item);
		listView.setAdapter(adapter);
	}

	private void retrieve() {
		accountTypes.clear();
		accountTypes.addAll(accountTypeService.getAll());
		adapter.notifyDataSetChanged();
	}

	private void showAddView() {
		final View view = getLayoutInflater().inflate(R.layout.account_type, null, false);
		final PopupWindow pw = new PopupWindow(view, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, true);
		pw.setContentView(view);
		pw.setAnimationStyle(android.R.style.Animation_Toast);
		pw.setFocusable(true);

		View parent = findViewById(R.id.account_type_manage_add);
		pw.showAtLocation(parent, Gravity.CENTER, 0, 0);

		Button ok = (Button) view.findViewById(R.id.account_type_ok);
		ok.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				EditText editText = (EditText) view.findViewById(R.id.account_type_value);
				String typeName = editText.getText().toString();
				if (StringUtils.isBlank(typeName)) {
					ToastUtils.showLong(getApplicationContext(), "新类型不能为空");
					return;
				}
				accountTypeService.save(new AccountType(typeName, R.drawable.account_other, ""));
				retrieve();
				pw.dismiss();
			}
		});

		// 关闭alert对话框架
		Button cancel = (Button) view.findViewById(R.id.account_type_cancel);
		cancel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				pw.dismiss();
			}
		});
	}

	private void remove(final int id, String name) {

		final AlertDialog dialog = new Builder(AccountTypeActivity.this).create();
		dialog.show();
		final Window window = dialog.getWindow();
		window.setLayout(android.view.WindowManager.LayoutParams.FILL_PARENT, android.view.WindowManager.LayoutParams.WRAP_CONTENT);
		window.setContentView(R.layout.acooly_dialog);
		TextView textView = (TextView) window.findViewById(R.id.acooly_dialog_content);
		textView.setText("确定删除类型: [" + name + "]");
		Button okButton = (Button) window.findViewById(R.id.acooly_dialog_ok);
		okButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.cancel();
				accountTypeService.delete(id);
				retrieve();
			}
		});

		Button cancelButton = (Button) window.findViewById(R.id.acooly_dialog_cancel);
		cancelButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.cancel();
			}
		});

	}

	class AccountTypeAdapter extends SimpleAdapter {
		private LayoutInflater mInflater;
		private List<AccountType> data = new ArrayList<AccountType>();
		private int resource;

		public AccountTypeAdapter(Context context, List<AccountType> data, int resource) {
			super(context, null, resource, null, null);
			this.data = data;
			this.resource = resource;
			this.mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return data.size();
		}

		@Override
		public Object getItem(int position) {
			return data.get(position);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(resource, null);
				holder.icon = (ImageView) convertView.findViewById(R.id.account_type_item_icon);
				holder.name = (TextView) convertView.findViewById(R.id.account_type_item_name);
				holder.comment = (TextView) convertView.findViewById(R.id.account_type_item_comment);
				holder.delete = (ImageView) convertView.findViewById(R.id.account_type_item_delete);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			final AccountType accountType = data.get(position);
			holder.icon.setImageBitmap(BitmapFactory.decodeResource(getResources(), accountType.getIcon()));
			holder.name.setText(accountType.getName());
			holder.comment.setText(accountType.getComment());
			holder.delete.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					remove(accountType.getId(), accountType.getName());
				}
			});
			return convertView;
		}

	}

	class ViewHolder {
		public ImageView icon;
		public TextView name;
		public TextView comment;
		public ImageView delete;
	}

}
